/**
 * 积分商城-兑换记录
 */
'use strict';
import React, {
    Component
} from 'react'
import {
    Link
} from 'react-router'

import Header from './Common/Header'
import imgErr from '../Img/other/splitImg.jpg'
import LoadingBox from './Common/LoadingBox'
import {
    HttpService,
    Tips,
    pullDownRefresh,
    dateFormat
} from '../Tools'

import product01 from '../Img/mall/product01.png'
import product02 from '../Img/mall/product02.png'
import product03 from '../Img/mall/product03.png'
import product04 from '../Img/mall/product04.png'
import product05 from '../Img/mall/product05.png'
import product06 from '../Img/mall/product06.png'


export default class Umtauschen extends Component {
    constructor(props) {
        super(props);
        this.page = 1;
        this.pagesize = 10;
        this.toBottom = 60;
        this.state = {
            dataList: [],
            dataLoad: 1,
            dataNull: 0,
            dataEnd: 0
        }
    }

    componentWillMount() {
        this.getData()
    }

    componentDidMount() {
        //加载更多
        window.onscroll = () => {
            const {
                dataEnd,
                dataNull,
                page
            } = this.state;
            if (this.dataLoad == 1 || dataEnd == 1 || dataNull == 1)
                return;
            var scrollTop = 0;
            if (document.documentElement && document.documentElement.scrollTop) {
                scrollTop = document.documentElement.scrollTop;
            } else if (document.body) {
                scrollTop = document.body.scrollTop;
            }
            if (document.body.clientHeight - window.innerHeight - scrollTop <= this.toBottom) {
                this.page++;
                this.getData();
            }
        }
        const obj = document.getElementById('refreshbox')
        const contentObj = document.getElementById('pdownrefresh')
        const statusObj = document.getElementById('status')
        let rct = this;
        pullDownRefresh(obj, contentObj, statusObj, 100, function(data) {
            if (data.status == 'end' && data.top >= 100 - 20)
                rct.getData(1);
        })
    }

    //获取数据列表
    getData(status) {
        this.dataLoad = 1
        this.setState({
            dataLoad: 1,
            dataEnd: 0,
            dataNull: 0
        });
        if (status == 1)
            this.page = 1
        HttpService.query({
            // url: '/src/Data/integral/Umtauschen.json',
            url: '/jf/peoplecard/pageCreditConsumeRecord',
            data: {
                token: sessionStorage.appToken,
                page: this.page,
                size: this.pagesize
            },
            success: (res => {
                this.dataLoad = 0
                this.setState({
                    dataLoad: 0
                });
                console.log(res);
                if (res.code == 50006) {
                    //获取数据成功
                    let dataList = res.data.list
                    if (status != 1)
                        dataList = this.state.dataList.concat(res.data.list);
                    this.setState({
                        dataList
                    });
                    if (res.data.total == this.pagesize) {
                        this.setState({
                            dataEnd: 1
                        });
                    } else if (res.data.list.length < this.pagesize) {
                        this.setState({
                            dataEnd: 1
                        });
                        if (dataList.length == 0) {
                            this.setState({
                                dataEnd: 0,
                                dataNull: 1
                            });
                        }
                    }
                    return;
                }
                Tips(res.msg);
            })
        })
    }

    render() {
        let copy = sessionStorage.userrole == '家长' ? '积分' : '荣誉值';
        const {
            dataList,
            dataLoad,
            dataNull,
            dataEnd
        } = this.state;
        let dataLists = dataList.map(function(item) {
            const time = dateFormat(new Date(item.createTime), '-');
            let url = imgErr
            if (item.smallImg.indexOf('{') > -1)
                url = JSON.parse(item.smallImg).goodsHeaderImg
            return (
                <li>
                    <img src={url} className="img"/>
                    <span className="produnct">
                        <span className="name">{item.goodsArchName}</span>
                        <span className="punkte">{item.consumeCredit}<span>{copy}</span></span>
                    </span>
                    <span className="punkte">
                        <span className="time">{time}</span>
                        <span>兑换成功</span>
                     </span>
                </li>
            )
        })
        return (
            <div className="umtauschen">
                <Header title="兑换记录"/>
                <div id='refreshbox'>
                    <div id='status'><span></span></div>
                    <div id='pdownrefresh'>
                        <div className="umtauschen_list">
                            <ul>
                                {dataLists}
                                <LoadingBox dataLoad={dataLoad} dataNull={dataNull} dataEnd={dataEnd} dataNullMsg='暂无兑换记录' />
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        )
    }
}
